<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>登录 - Model Config Center</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
      :root{
        --pri:#2E64E8; --pri-2:#20B2AA; --fg:#111827; --muted:#6b7280; --bd:#e5e7eb;
      }
      *{box-sizing:border-box}
      html,body{height:100%;margin:0}
      body{font-family:Inter,system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif;background:linear-gradient(135deg, #2E64E8 0%, #38bdf8 50%, #20B2AA 100%);display:flex;align-items:center;justify-content:center}
      .container{background:#fff;border-radius:16px;box-shadow:0 20px 60px rgba(0,0,0,.2);padding:40px;width:100%;max-width:420px}
      .logo{width:48px;height:48px;border-radius:10px;background:linear-gradient(135deg,#0A59F7,#38bdf8);margin:0 auto 24px;box-shadow:0 4px 12px rgba(0,0,0,.15)}
      h1{margin:0 0 8px;font-size:24px;font-weight:700;text-align:center;color:#111827}
      .subtitle{text-align:center;color:#6b7280;font-size:14px;margin-bottom:32px;font-weight:500}
      .tabs{display:flex;gap:8px;margin-bottom:24px;border-bottom:1px solid #e5e7eb}
      .tab{padding:12px 20px;background:transparent;border:none;cursor:pointer;font-weight:600;font-size:14px;color:#6b7280;border-bottom:2px solid transparent}
      .tab.active{color:#2E64E8;border-bottom-color:#2E64E8}
      .form{display:none}
      .form.active{display:block}
      label{display:block;margin:16px 0 8px;font-weight:700;font-size:13px;color:#374151}
      input{width:100%;border:1px solid #e5e7eb;background:#fff;color:#111827;padding:12px 14px;border-radius:8px;font-size:14px;font-weight:500}
      input:focus{outline:none;border-color:#2E64E8;box-shadow:0 0 0 3px rgba(46,100,232,.1)}
      .btn-primary{width:100%;border:none;background:linear-gradient(180deg,#2E64E8,#20B2AA);color:#fff;padding:14px;border-radius:8px;font-weight:700;font-size:15px;cursor:pointer;margin-top:24px;box-shadow:0 4px 12px rgba(46,100,232,.3)}
      .btn-primary:hover{box-shadow:0 6px 16px rgba(46,100,232,.4);transform:translateY(-1px)}
      .btn-primary:disabled{opacity:.6;cursor:not-allowed;transform:none}
      .error{color:#dc2626;font-size:13px;margin-top:8px;font-weight:600;display:none}
      .error.show{display:block}
      .success{color:#16a34a;font-size:13px;margin-top:8px;font-weight:600;display:none}
      .success.show{display:block}
    </style>
  </head>
  <body>
    <div class="container">
      <div class="logo"></div>
      <h1>OpenHarmony</h1>
      <p class="subtitle">Model Config Center</p>
      
      <div class="tabs">
        <button class="tab active" onclick="switchTab('login')">登录</button>
        <button class="tab" onclick="switchTab('register')">注册</button>
      </div>

      <div id="loginForm" class="form active">
        <label>用户名</label>
        <input type="text" id="loginUsername" placeholder="请输入用户名" />
        <label>密码</label>
        <input type="password" id="loginPassword" placeholder="请输入密码" />
        <div class="error" id="loginError"></div>
        <button class="btn-primary" onclick="doLogin()">登录</button>
      </div>

      <div id="registerForm" class="form">
        <label>用户名</label>
        <input type="text" id="regUsername" placeholder="3-20个字符" />
        <label>邮箱</label>
        <input type="email" id="regEmail" placeholder="your@email.com" />
        <label>密码</label>
        <input type="password" id="regPassword" placeholder="至少8个字符" />
        <div class="error" id="regError"></div>
        <div class="success" id="regSuccess"></div>
        <button class="btn-primary" onclick="doRegister()">注册</button>
      </div>
    </div>

    <script>
      function switchTab(tab){
        document.querySelectorAll('.tab').forEach(t=>t.classList.remove('active'));
        document.querySelectorAll('.form').forEach(f=>f.classList.remove('active'));
        if(tab==='login'){
          document.querySelector('.tab').classList.add('active');
          document.getElementById('loginForm').classList.add('active');
        } else {
          document.querySelectorAll('.tab')[1].classList.add('active');
          document.getElementById('registerForm').classList.add('active');
        }
        document.querySelectorAll('.error').forEach(e=>e.classList.remove('show'));
        document.querySelectorAll('.success').forEach(e=>e.classList.remove('show'));
      }

      async function doLogin(){
        const username=document.getElementById('loginUsername').value.trim();
        const password=document.getElementById('loginPassword').value;
        const err=document.getElementById('loginError');
        err.classList.remove('show');
        if(!username || !password){ err.textContent='请填写用户名和密码'; err.classList.add('show'); return; }
        try{
          const res=await fetch('/api/auth/login',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({username,password})});
          if(res.ok){
            const data=await res.json();
            localStorage.setItem('token',data.token);
            localStorage.setItem('username',data.username);
            localStorage.setItem('roles',JSON.stringify(data.roles));
            location.href='/';
          } else { err.textContent='用户名或密码错误'; err.classList.add('show'); }
        } catch(e){ err.textContent='登录失败，请重试'; err.classList.add('show'); }
      }

      async function doRegister(){
        const username=document.getElementById('regUsername').value.trim();
        const email=document.getElementById('regEmail').value.trim();
        const password=document.getElementById('regPassword').value;
        const err=document.getElementById('regError');
        const suc=document.getElementById('regSuccess');
        err.classList.remove('show');
        suc.classList.remove('show');
        if(!username || !email || !password){ err.textContent='请填写所有字段'; err.classList.add('show'); return; }
        if(password.length<8){ err.textContent='密码至少8个字符'; err.classList.add('show'); return; }
        try{
          const res=await fetch('/api/auth/register',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({username,email,password})});
          if(res.ok){
            const data=await res.json();
            suc.textContent='注册成功！正在登录...';
            suc.classList.add('show');
            setTimeout(()=>{
              localStorage.setItem('token',data.token);
              localStorage.setItem('username',data.username);
              localStorage.setItem('roles',JSON.stringify(data.roles));
              location.href='/';
            },1000);
          } else {
            const errData=await res.json().catch(()=>({}));
            err.textContent=errData.message||'注册失败，用户名或邮箱可能已存在';
            err.classList.add('show');
          }
        } catch(e){ err.textContent='注册失败，请重试'; err.classList.add('show'); }
      }

      document.querySelectorAll('input').forEach(i=>i.addEventListener('keypress',e=>{if(e.key==='Enter'){if(e.target.closest('#loginForm'))doLogin();else doRegister();}}));
    </script>
  </body>
</html>

